package com.regal.packet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.xmlpull.v1.XmlPullParser;

/**
 *
 */
public class TradeList extends IQ {

	private final List<TradeItem> comItems = new ArrayList<TradeItem>();
	private String tradeType;

    /**
     * Element name of the packet extension.
     */
    public static final String ELEMENT_NAME = "query";

    /**
     * Namespace of the packet extension.
     */
    public static final String NAMESPACE = "jabber:iq:trade_list";
    
    public void addTradeItem(TradeItem item) {    	
    	synchronized (comItems) {
    		comItems.add(item);
        }    	
    }
    
    public int getTradeItemCount() {
        synchronized (comItems) {
            return comItems.size();
        }
    }
    
    public Collection<TradeItem> getTradeItems() {
        synchronized (comItems) {
            return Collections.unmodifiableList(new ArrayList<TradeItem>(comItems));
        }
    }

    public String getChildElementXML() {
        StringBuffer buf = new StringBuffer();
        buf.append("<query xmlns=\"" + NAMESPACE + "\"");
        buf.append(" type=\""+tradeType+"\"");
        buf.append(" />");
        return buf.toString();
    }

    
    
    /**
     * A Company item, which consists of a companyid...
     */
    public static class TradeItem {

    	private String tradeId;
    	private String tradeName;
    	private String level;
    	private String parentid;
    	        
    	public TradeItem() {
        
        }
        /**
         * Creates a new company item.
         *
         * @param user the companyid.
         * @param name the companyname.
         */
        public TradeItem(String _tradeName, String _level, String _parentid) {
            this.tradeName = _tradeName;
        	this.level = _level;
        	this.parentid = _parentid;
        }
		public String getLevel() {
			return level;
		}
		public void setLevel(String level) {
			this.level = level;
		}
		public String getParentid() {
			return parentid;
		}
		public void setParentid(String parentid) {
			this.parentid = parentid;
		}
		public String getTradeName() {
			return tradeName;
		}
		public void setTradeName(String tradename) {
			this.tradeName = tradename;
		}
		public String getTradeId() {
			return tradeId;
		}
		public void setTradeId(String tradeId) {
			this.tradeId = tradeId;
		}


		
    }
    /**
     * An IQProvider for TradeListIQ packet.
     *
     * @author Derek DeMoro
     */
    public static class Provider implements IQProvider {

        public Provider() {
            super();
        }

        public IQ parseIQ(XmlPullParser parser) throws Exception {
            
        	TradeList tradeList = new TradeList();
        	TradeItem tradeItem = null;

            boolean done = false;
            while (!done) {
                int eventType = parser.next();
                if (eventType == XmlPullParser.START_TAG) {
                	if (parser.getName().equals("item")) {
                		tradeItem = new TradeItem();
                		String tradeId = parser.getAttributeValue("", "tradeid");
                        tradeItem.setTradeId(tradeId);
                        String tradeName = parser.getAttributeValue("", "tradename");
                        tradeItem.setTradeName(tradeName);
                        String level = parser.getAttributeValue("", "level");
                        tradeItem.setLevel(level);
                        String parentid = parser.getAttributeValue("", "parentid");
                        tradeItem.setParentid(parentid);
                    }
                }

                else if (eventType == XmlPullParser.END_TAG) {
                	if (parser.getName().equals("item")) {
                		tradeList.addTradeItem(tradeItem);
                		System.out.println("get trade item tradename:"+tradeItem.getTradeName()+":tradeid:"+tradeItem.getTradeId()+":level:"+tradeItem.getLevel());
                    }
                    if (parser.getName().equals(ELEMENT_NAME)) {
                        done = true;
                    }
                }
            }

            return tradeList;
        }
    }
	public String getTradeType() {
		return tradeType;
	}

	public void setTradeType(String type) {
		this.tradeType = type;
	}

   
}
